package com.geor.gcv.core.process;

import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

/**
 * 旋转图片
 * 
 * @author geor.lusongnan
 *
 */
public class RotateProcess extends Process {
	private Mat img;
	private Process process;
	/** true: 顺时针 */
	private boolean direction = true;
	/** 围绕哪个点旋转 */
	private Point point;
	/** 旋转角度 */
	private double degree;

	public void setPoint(Point point) {
		this.point = point;
	}

	public RotateProcess(Process process, boolean direction, double degree) {
		super();
		this.process = process;
		this.direction = direction;
		this.degree = degree;
	}

	public RotateProcess(Mat img, boolean direction, double degree) {
		super();
		this.img = img;
		this.direction = direction;
		this.degree = degree;
	}

	public RotateProcess(Process process, double degree) {
		super();
		this.process = process;
		this.degree = degree;
	}

	public RotateProcess(Mat img, double degree) {
		super();
		this.img = img;
		this.degree = degree;
	}

	public RotateProcess(Process process, boolean direction, Point point, double degree) {
		super();
		this.process = process;
		this.direction = direction;
		this.point = point;
		this.degree = degree;
	}

	public RotateProcess(Mat img, boolean direction, Point point, double degree) {
		super();
		this.img = img;
		this.direction = direction;
		this.point = point;
		this.degree = degree;
	}

	@Override
	public String getProcessName() {
		if (img != null)
			return "旋转图像";
		return process.getProcessName() + "-->旋转图像";
	}

	@Override
	public Mat process() {
		if (process != null) {
			img = process.process();
		}

		Mat result = new Mat();
		if (point == null) {
			point = new Point(img.cols() / 2, img.rows() / 2);
		}
		Mat dst = Imgproc.getRotationMatrix2D(point, (direction ? -1 : 1) * degree, Imgproc.INTER_LINEAR);
		Imgproc.warpAffine(img, result, dst, new Size(img.cols(), img.rows()));
		dst.release();
		return result;
	}
}
